/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


/**
 *
 * @author Eng Omnia
 */
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.geom.AffineTransform;
import javax.swing.*;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;

public class Board extends JPanel implements ActionListener {

    private Player p;
    Image sand, green, stone, door, bomb, victim, stairs, n, s, w, e, h, js, k, l;
    Timer time;
    char[][] level;
    Level[] levels;
    int levelNo;
    JFrame frame;
    JDialog dialog;
    boolean[][][] solve;
    int type;
    boolean enter = false;

    public Board() {
        //0 for interactive
        //1 for queue
        // 2 forstack

        GenerateLevel allLevels = new GenerateLevel("C:\\My Documents\\map.txt");
        this.levels = allLevels.getLevels();
        int start = 0;
        for (start = 0; start < 9; start++) {
            if (levels[start] != null) {
                if (levels[start].hasS) {
                    break;
                }
            }
        }
        levelNo = start;
        this.level = levels[start].getLevel();
        //frame.setSize(this.level.length*40+40, this.level.length*40+40);
        addKeyListener(new AL());
        p = new Player(this);
        p.setX(levels[start].getStaX());
        p.setY(levels[start].getStaY());
        setFocusable(true);
        ImageIcon sandA = new ImageIcon("C:\\Users\\Eng Omnia\\Pictures\\project\\sand.png");
        ImageIcon greenA = new ImageIcon("C:\\Users\\Eng Omnia\\Pictures\\project\\green.png");
        ImageIcon stoneA = new ImageIcon("C:\\Users\\Eng Omnia\\Pictures\\project\\stone.png");
        ImageIcon Vict = new ImageIcon("C:\\Users\\Eng Omnia\\Pictures\\project\\victim.png");
        ImageIcon bom = new ImageIcon("C:\\Users\\Eng Omnia\\Pictures\\project\\bomb.png");
        ImageIcon doo = new ImageIcon("C:\\Users\\Eng Omnia\\Pictures\\project\\gate.png");
        ImageIcon stai = new ImageIcon("C:\\Users\\Eng Omnia\\Pictures\\project\\stairs.png");
        stone = stoneA.getImage();
        green = greenA.getImage();
        sand = sandA.getImage();
        victim = Vict.getImage();
        door = doo.getImage();
        bomb = bom.getImage();
        stairs = stai.getImage();
        time = new Timer(110, this);
        time.start();



    }

    public void setType(int type) {
        this.type = type;
    }

    public int getType() {
        return type;
    }

    public void setForQ() {
       Level[]temps= new Level[10];
this.setLevel(levels[levelNo].getLevel());
        TermQ Start = new TermQ(levels[levelNo].getStaY(), levels[levelNo].getStaX());
        QueueAlgorithmFllow x = new QueueAlgorithmFllow();
        TermQ answer = x.QueueAlgoPath(levels, Start);
        autoSolve solveQ = new autoSolve();
        solveQ.makePath(levels, answer);
        this.solve = solveQ.path;
        int index = 0;
        for (int num = 0; num < solve.length ; num++) {
            if (solve[num] != null) {
                char[][] currentChar = levels[num].getLevel();
                boolean[][] currentBoolean = solve[num];
                for (int i = 0; i < currentChar.length; i++) {
                    for (int j = 0; j < currentChar.length; j++) {
                      
                        if (currentChar[i][j] == ' ' && currentBoolean[i][j] == true) {
                            if (i - 1 >= 0 && currentBoolean[i - 1][j]) {
                             
                                currentChar[i - 1][j] = 'n';
                            } else if (j - 1 >= 0 && currentBoolean[i][j - 1]) {
                                currentChar[i - 1][j] = 'w';
                            } else if (i + 1 < currentBoolean.length && currentBoolean[i + 1][j]) {
                                currentChar[i - 1][j] = 's';

                            } else if (j + 1 < currentBoolean.length && currentBoolean[i][j + 1]) {
                                currentChar[i - 1][j] = 'e';

                            } else if (i - 1 >= 0 && j - 1 >= 0 && currentBoolean[i - 1][j - 1]) {
                                currentChar[i - 1][j - 1] = 'h';

                            } else if (i - 1 >= 0 && j + 1 < currentBoolean.length && currentBoolean[i - 1][j + 1]) {
                                currentChar[i - 1][j + 1] = 'k';

                            } else if (i - 1 >= 0 && j - 1 >= 0 && currentBoolean[i - 1][j - 1]) {
                                currentChar[i - 1][j - 1] = 'h';

                            } else if (i + 1 < currentBoolean.length && j - 1 >= 0 && currentBoolean[i + 1][j - 1]) {
                                currentChar[i + 1][j - 1] = 'k';

                            } else if (i + 1 < currentBoolean.length && j + 1 < currentBoolean.length && currentBoolean[i + 1][j + 1]) {
                                currentChar[i + 1][j + 1] = 'l';

                            }

                        }
                    }
                }
             

            }
         
        }
        for(index=0;index<levels.length&&!solveQ.s.isEmpty();index++){
                int newPlace = (Integer) solveQ.s.pop();
                System.out.println(newPlace);
temps[index]=levels[newPlace];  
        }




        levels=temps;
         levelNo=0;


    }

    public void setArrow() {
        ImageIcon up = new ImageIcon("C:\\Users\\Eng Omnia\\Pictures\\project\\up.png");
        ImageIcon down = new ImageIcon("C:\\Users\\Eng Omnia\\Pictures\\project\\down.png");

        ImageIcon left = new ImageIcon("C:\\Users\\Eng Omnia\\Pictures\\project\\left.png");
        ImageIcon right = new ImageIcon("C:\\Users\\Eng Omnia\\Pictures\\project\\right.png");
        ImageIcon upRight = new ImageIcon("C:\\Users\\Eng Omnia\\Pictures\\project\\upright.png");
        ImageIcon upLeft = new ImageIcon("C:\\Users\\Eng Omnia\\Pictures\\project\\upleft.png");
        ImageIcon downRight = new ImageIcon("C:\\Users\\Eng Omnia\\Pictures\\project\\downright.png");
        ImageIcon downLeft = new ImageIcon("C:\\Users\\Eng Omnia\\Pictures\\project\\downleft.png");
        n = up.getImage();
        s = down.getImage();
        e = right.getImage();
        w = left.getImage();
        h = upLeft.getImage();
        js = downLeft.getImage();
        k = upRight.getImage();
        l = downRight.getImage();
    }
public void shortestPath(){
    this.setForQ();
    this.setType(1);
    this.setArrow();
}
public void prepareNormal(int challange) {

for(int num= 0 ;num<levels.length;num++){
    int rand=(int) (2*challange * Math.random());
  if(levels[num]!=null){
     int count=0;
      char[][]curr= levels[num].getLevel();
      int length=curr.length;
     
    	  
      for(int i=0;i<length;i++){
          for(int j=0;j<length;j++){
              if(curr[i][j]==' '&&(i+j==rand)){
                  curr[i][j]='*';
                  count++;
              }
              System.out.print(curr[i][j]);
          }
          System.out.println("");
      }
      System.out.println("----------------------------");
      
      
  }
}       
  }
    public void setFrame(JFrame newFrame) {
        frame = newFrame;
    }

    public JFrame getFrame() {
        return frame;
    }

    public Player getPlayer() {
        return p;
    }

    public int getLevelNo() {
        return levelNo;
    }

    public void setLevelNo(int levelNo) {
        this.levelNo = levelNo;
    }

    public char[][] getLevel() {
        return level;
    }

    public void setLevel(char[][] newLevel) {
        level = newLevel;
    }

    public Level[] getLevels() {
        return levels;
    }

    public void setLevels(Level[] all) {
        levels = all;
    }

    public void setDialog(JDialog newDialog) {
        dialog = newDialog;
    }

    public JDialog getDialog() {
        return dialog;
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
        if (type == 0) {
            p.move(p.getX(), p.getY());
        }

        repaint();
    }

    @Override
    public void paint(Graphics g) {
if(enter){
    enter=false;
    levelNo++;
    if(levelNo<levels.length&&levels[levelNo]!=null){
    this.setLevel(levels[levelNo].getLevel());
    }
}
        //  this.frame.getContentPane().setBackground(Color.yellow);
        super.paint(g);

        if (level[p.getY()][p.getX()] == 'B' || level[p.getY()][p.getX()] == 'V') {

            dialog.setVisible(true);
            frame.setVisible(false);
        }
        this.getFrame().setSize(this.getLevel().length * 40 + 40, this.getLevel().length * 40 + 40);
        Graphics2D g2D = (Graphics2D) g;

        try {

            g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
            AffineTransform aTran = new AffineTransform();
            aTran.translate(1.0f, 1.0f);
            g2D.transform(aTran);

            for (int i = 0; i < level.length; i++) {
                for (int j = 0; j < level.length; j++) {
                    if (level[i][j] == 'x') {
                        g2D.drawImage(stone, (int) 40 * j, (int) 40 * i,
                                40, 40, this);
                    } else if (level[i][j] == 'r') {
                        g2D.drawImage(green, (int) 40 * j, (int) 40 * i,
                                (int) 40, (int) 40, this);
                    } else if (level[i][j] == ' ') {
                        g2D.drawImage(sand, (int) 40 * j, (int) 40 * i,
                                (int) 40, (int) 40, this);
                    } else if (level[i][j] == 'V') {
                        g2D.drawImage(victim, (int) 40 * j, (int) 40 * i,
                                (int) 40, (int) 40, this);
                    } else if (level[i][j] == 'B') {
                        g2D.drawImage(bomb, (int) 40 * j, (int) 40 * i,
                                (int) 40, (int) 40, this);
                    } else if (level[i][j] == 'S') {
                        g2D.drawImage(door, (int) 40 * j, (int) 40 * i,
                                (int) 40, (int) 40, this);
                    } else {
                        g2D.drawImage(stairs, (int) 40 * j, (int) 40 * i,
                                (int) 40, (int) 40, this);
                    }
                    if (type != 0) {
                        if (level[i][j] == 'n') {
                            g2D.drawImage(n, (int) 40 * j, (int) 40 * i,
                                    (int) 40, (int) 40, this);
                        } else if (level[i][j] == 's') {
                            g2D.drawImage(s, (int) 40 * j, (int) 40 * i,
                                    (int) 40, (int) 40, this);
                        } else if (level[i][j] == 'w') {
                            g2D.drawImage(w, (int) 40 * j, (int) 40 * i,
                                    (int) 40, (int) 40, this);
                        } else if (level[i][j] == 'h') {
                            g2D.drawImage(w, (int) 40 * j, (int) 40 * i,
                                    (int) 40, (int) 40, this);
                        } else if (level[i][j] == 'j') {
                            g2D.drawImage(js, (int) 40 * j, (int) 40 * i,
                                    (int) 40, (int) 40, this);
                        } else if (level[i][j] == 'k') {
                            g2D.drawImage(k, (int) 40 * j, (int) 40 * i,
                                    (int) 40, (int) 40, this);
                        } else if (level[i][j] == 'l') {
                            g2D.drawImage(l, (int) 40 * j, (int) 40 * i,
                                    (int) 40, (int) 40, this);
                        }
                    }

                }
            }
            if (type == 0) {
                g2D.drawImage(p.getPlayer(), p.getX() * 40, p.getY() * 40, 40, 40, this);
            }
        } catch (Exception e) {
        }

        paintComponents(g);

    }

    private class AL extends KeyAdapter {

        public void keyTyped(KeyEvent e) {
            //   throw new UnsupportedOperationException("Not supported yet.");
        }

        public void keyPressed(KeyEvent e) {
            //throw new UnsupportedOperationException("Not supported yet.");

            p.keyPressed(e);
            if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                enter = true;
            }
        }

        public void keyReleased(KeyEvent e) {
            //throw new UnsupportedOperationException("Not supported yet.");
            p.keyReleased(e);
        }
    }
    public static void main(String []args){
    Board b= new Board();
    b.prepareNormal(5);
    }
}
